package cn.fl.audit.service.impl;

import cmm.comm.facade.flow.condition.SysFlowRejRsnQueryCondition;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.audit.dto.AudRejectOpinionDTO;
import cn.fl.audit.dto.AudStandingBookDTO;
import cn.fl.audit.service.IAudRejectOpinionService;
import cn.fl.audit.service.IAuditApiService;
import cn.fl.audit.transform.AudRejOpnPageVOTransform;
import cn.fl.audit.transform.AudRejectOpinionVOTransform;
import cn.fl.audit.transform.AudStandingBookVOTransform;
import cn.fl.audit.vo.AudRejectOpinionPageVO;
import cn.fl.audit.vo.AudRejectOpinionVO;
import cn.fl.audit.vo.AudStandingBookVO;
import cn.fl.flow.service.ISysFlowRejRsnService;
import cn.fl.flow.vo.SysFlowRejRsnVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.preloan.IAuditRejectOpinionService;
import cn.fl.preloan.condition.AuditRejectOpQueryCondition;
import cn.fl.preloan.dto.audit.AudRejectOpPageRequest;
import cn.fl.preloan.dto.audit.AudRejectOpPageResponseDTO;
import cn.fl.preloan.dto.audit.AuditRejectOpinionDTO;
import cn.fl.commons.result.ResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 驳回意见 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AudRejectOpinionServiceImpl implements IAudRejectOpinionService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AudRejectOpinionServiceImpl.class);

	/**
	 */
	@Autowired
	private IAuditApiService auditApiService;

	/**
	 * 流程驳回意见本地服务
	 */
	@Autowired
	private ISysFlowRejRsnService sysFlowRejRsnService;

	@Autowired
	private IAuditRejectOpinionService auditRejectOpinionService;

	@Override
	public AudRejectOpinionVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		AudRejectOpinionDTO dto = null;//this.自定义ApiService.selectById(id);
		return AudRejectOpinionVOTransform.toVO(dto);
	}

	@Override
	public Long add(AudRejectOpinionVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.audRejectOpinionApiService.add(AudRejectOpinionVOTransform.toDTO(entity));
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.audRejectOpinionApiService.removeById(id);
		return false;
	}

	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AudRejectOpinionVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.audRejectOpinionApiService.modify(AudRejectOpinionVOTransform.toDTO(entity));
		return false;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();

		List<AudRejectOpinionVO> VOList = new ArrayList<>();
		AudRejectOpPageRequest pageRequest = new AudRejectOpPageRequest((AuditRejectOpQueryCondition) page.getCondition(), page.getCurrentPage(), page.getEveryPage());
		ResponseData<AudRejectOpPageResponseDTO> responseData = auditRejectOpinionService.searchListPage(pageRequest);
		if (responseData != null && ResponseData.SUCCESS_CODE.equals(responseData.getCode()) && responseData.getData() != null) {
			AudRejectOpPageResponseDTO data = responseData.getData();
			List<AuditRejectOpinionDTO> dtoList = data.getAuditRejectOpinionDTOS();
			for (AuditRejectOpinionDTO dto : dtoList) {
				AudRejectOpinionVO audRejectOpinionVO = new AudRejectOpinionVO();
				BeanUtils.copyProperties(dto, audRejectOpinionVO);
				audRejectOpinionVO.setCapPayId(dto.getPayId());
				VOList.add(audRejectOpinionVO);
			}
		}
		//根据驳回意见ID设置驳回意见详情DESC
		List<SysFlowRejRsnVO> sysFlowRejRsnVOList = this.sysFlowRejRsnService.searchList(new SysFlowRejRsnQueryCondition());
		if(CollectionUtils.isNotEmpty(sysFlowRejRsnVOList) && CollectionUtils.isNotEmpty(VOList)){
			for (AudRejectOpinionVO audRejectOpinionVO : VOList) {
				//for (SysFlowRejRsnVO sysFlowRejRsnVO : sysFlowRejRsnVOList) {
					String parentPath = audRejectOpinionVO.getParentPath();
					if(StringUtils.isNotEmpty(parentPath)){
						String[] parentPathSplit = parentPath.split(";");
						String parentPathDesc = "";
						if(parentPathSplit.length>0){
							for (String string : parentPathSplit) {//遍历
								for (SysFlowRejRsnVO sysFlowRejRsnVO : sysFlowRejRsnVOList) {
									if(null!=string && !"null".equals(string) && Long.valueOf(string).equals(sysFlowRejRsnVO.getId())){
										parentPathDesc+=sysFlowRejRsnVO.getRsnDesc();
										parentPathDesc+=";";
									}
								}
							}
							audRejectOpinionVO.setParentPathDesc(parentPathDesc.substring(0, parentPathDesc.length()-1));
						}
					}

					String selectedValue = audRejectOpinionVO.getSelectedValue();
					if(StringUtils.isNotEmpty(selectedValue)){
						String[] selectedValueSplit = selectedValue.split(";");
						String selectedValueDesc = "";
						if(selectedValueSplit.length>0){
							for (String string : selectedValueSplit) {
								for (SysFlowRejRsnVO sysFlowRejRsnVO : sysFlowRejRsnVOList) {
									if(null!=string && !"null".equals(string) && Long.valueOf(string).equals(sysFlowRejRsnVO.getId())){
										selectedValueDesc+=sysFlowRejRsnVO.getRsnDesc();
										selectedValueDesc+=";";
									}
								}
							}
							audRejectOpinionVO.setSelectedValueDesc(selectedValueDesc.substring(0, selectedValueDesc.length()-1));
						}
					}
				//}
			}
		}

		List<AudRejectOpinionPageVO> PageVOList = AudRejOpnPageVOTransform.toPageVOList(VOList);
		page.setResultList(PageVOList);
		return page;
	}

	@Override
	public boolean batchAdd(List<AudRejectOpinionVO> VOList,String bookTypCd) throws BaseException {
		if(CollectionUtils.isEmpty(VOList)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		return this.auditApiService.batchAdd(AudRejectOpinionVOTransform.toDTOList(VOList),bookTypCd,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}

	@Override
	public boolean batchAddNull(AudStandingBookVO bookVO, String bookTypCd)
			throws BaseException {
		if(null==bookVO || StringUtils.isEmpty(bookTypCd)){
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		AudStandingBookDTO bookDTO = AudStandingBookVOTransform.toDTO(bookVO);
		return this.auditApiService.batchAddNull(bookDTO, bookTypCd,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
	}

	/*@Override
	public List<AudRejectOpinionVO> searchList(AudRejectOpinionQueryCondition qc) throws BaseException {
		if(null==qc) qc = new AudRejectOpinionQueryCondition();
		List<AudRejectOpinionDTO> DTOList = this.auditApiService.searchAudRejectOpinionList(qc, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
		return AudRejectOpinionVOTransform.toVOList(DTOList);
	}*/
}
